class Admin::UsersController < Admin::BaseController
  include PaginatorHelper
  
  def index
    parameters = create_parameters(params)
    @accounts = Admin.find_users_for_admin_by_parameters(parameters)
    
    
    
    if(params[:page] != nil) then
      @page = params[:page].to_i
    else
      @page = 0 
    end
    hash_product_list = get_shortened_list(@accounts,@page)
    @accounts = hash_product_list[:object]
    @indices = hash_product_list[:indices]
  end
  
  def edit
  end
  def show
    @account = User.first(params[:id])
    @addresses = Address.find(:all, :conditions => ['user_id = ?',@account.id])
    
  end
  
  def update
    @user = User.find(params[:user][:id])
    @user.update_attributes(params[:user])
    @user.save
    create_parameters(params)
    redirect_to(admin_users_path(@params))
  end
  
  def destroy
    @user = User.find(params[:id])
    @user.destroy
    flash[:notice] = "Account verwijdert"
    create_parameters(params)
    redirect_to(admin_users_path(@params))
  end
  
  #creeert de parameters die worden meegegeven om de textvelden in de index ingevuld te houden.
  private
  def create_parameters(params)
    parameters = {:search=>{:and=>{"users.name" => {:like => ""},"users.first_name" => {:like => ""},"users.surname" => {:like => ""},"addresses.city" => {:like => ""},"addresses.postal_code" =>{:eq => ""},"addresses.house_number" => {:eq => ""},"users.email" => {:like => ""}, :DATE=>{'users.created_at' =>{:gt => nil, :st => nil}}}}, :sort => "", :sort_on => ""}
    @params = {}
    if(params == nil)
      params = {}
    end
    if(params[:user] != nil)
      parameters[:search][:and]['users.name'][:like] = params[:user]
      @params[:user] = params[:user]
      @user = params[:user]
    end
    if(params[:first_name] != nil)
      parameters[:search][:and]['users.first_name'][:like] = params[:first_name]
      @params[:first_name] = params[:first_name]
      @first_name = params[:first_name]
    end
    if(params[:surname] != nil)
      parameters[:search][:and]['users.surname'][:like] = params[:surname]
      @params[:surname] = params[:surname]
      @surname = params[:surname]
    end
    if(params[:city] != nil)
      parameters[:search][:and]['addresses.city'][:like] = params[:city]
      @params[:city] = params[:city]
      @city = params[:city]
    end
    if(params[:postal_code] != nil)
      parameters[:search][:and]['addresses.postal_code'][:like] = params[:postal_code]
      @params[:postal_code] = params[:postal_code]
      @postal_code = params[:postal_code]
    end
    if(params[:email] != nil)
      parameters[:search][:and]['users.email'][:like] = params[:email]
      @params[:email] = params[:email]
      @email = params[:email]
    end
    if(params[:city] != nil)
      parameters[:search][:and]['addresses.city'][:like] = params[:city]
      @params[:city] = params[:city]
      @city = params[:city]
    end
    if(params[:to] != nil)
      parameters[:search][:and][:DATE]['users.created_at'][:st] = params[:to]
      date = params[:to]
      if(Date.valid_civil?(date[:year].to_i,date[:month].to_i,date[:day].to_i))
          @date_to = Date.new(date[:year].to_i,date[:month].to_i,date[:day].to_i)
      end
      @params[:to] = params[:to]
    end
    if(params[:from] != nil)
      parameters[:search][:and][:DATE]['users.created_at'][:gt] = params[:from]
      date = params[:from]
      if(Date.valid_civil?(date[:year].to_i,date[:month].to_i,date[:day].to_i))
          @date_from = Date.new(date[:year].to_i,date[:month].to_i,date[:day].to_i)
      end
      @params[:from] = params[:from]
    end
    if(params[:sort] != nil)
      parameters[:sort] = params[:sort]
      @params[:sort] = params[:sort]
    end
    if(params[:sort_on] != nil)
      parameters[:sort_on] = params[:sort_on]
      @params[:sort_on] = params[:sort_on]
    end
    if(params[:page] != nil)
      @params[:page] = params[:page]
    end
    return parameters
  end
end